#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2018-02-01 14:34:01

import select
import time
import sys


def tailLog(logfile):
    try:
        with open(logfile) as f:
            f.seek(0, 2)
            while True:
                # f.seek(cur_offest)
                data = f.readline()
                if data == '':
                    continue
                else:
                    sys.stdout.write(data)
                    # cur_offest = f.tell()
                time.sleep(1)
        '''
        if cur_offest == os.stat(logfile).st_size:
            while True:
                if cur_offest < os.stat(logfile).st_size:
                    break
                else:
                    continue
        else:
            tailLog(logfile, cur_offest)
        '''
    except FileNotFoundError:
        print('%s not found' % logfile)


def tailLogV2(logfile, offest):
    epoll_fd = select.epoll()
    cur_offest = offest
    with open(logfile) as f:
        f.seek(cur_offest, 0)
        epoll_fd.register(f.fileno(), select.EPOLLIN)
        while True:
            epoll_list = epoll_fd.poll()
            for fd, events in epoll_list:
                if fd == f.fileno():
                    data = f.readline()
                    if data == '':
                        continue
                    else:
                        print(data)
                        cur_offest = f.tell()
                else:
                    continue


if __name__ == '__main__':
    tailLog('test.log')
